package com.hosp.controller.v1;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.hosp.bean.Sysmenu;
import com.hosp.service.SysResourceService;
import com.hosp.vo.PageRequestVO;
import com.hosp.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统资源管理 API
 *
 * @author ztranscript 2021/9/12 21:31
 **/
@RestController
@RequestMapping("/resource")
public class SysResourceController {
    @Autowired
    private SysResourceService resourceService;

    /**
     * 查询列表
     *
     * @param sysmenu     查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    @GetMapping("/list")
    public ResultVO<Sysmenu> list(Sysmenu sysmenu, PageRequestVO pageRequest) {
        PageInfo<Sysmenu> pageInfo = resourceService.list(sysmenu, pageRequest);
        return new ResultVO<>(pageInfo);
    }

    /**
     * 查询列表
     *
     * @param mId 查询条件
     * @return 响应
     */
    @GetMapping("/{mId}")
    public ResultVO<Sysmenu> selectById(@PathVariable("mId") Integer mId) {
        Sysmenu sysmenu = resourceService.selectById(mId);
        return new ResultVO<>(sysmenu);
    }

    /**
     * 保存
     *
     * @param sysmenu 保存对象
     * @return 响应
     */
    @PostMapping("/save")
    public ResultVO<String> save(Sysmenu sysmenu) {
        boolean flag = resourceService.save(sysmenu);

        if (flag) {
            return new ResultVO<>(200, "保存成功");
        } else {
            return new ResultVO<>(500, "保存失败");
        }
    }

    /**
     * 删除
     *
     * @param mIdStr 待删除 IDs
     * @return 响应
     */
    @PostMapping("/delete")
    public ResultVO<String> delete(String mIdStr) {
        List<Integer> mIds = new ArrayList<>();
        String[] split = mIdStr.split(",");
        for (String s : split) {
            mIds.add(Integer.valueOf(s));
        }
        boolean flag = resourceService.delete(mIds);

        if (flag) {
            return new ResultVO<>(200, "删除成功");
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }

    @GetMapping("/check")
    public ResultVO<Boolean> checkName(String mName) {
        boolean flag = resourceService.check(mName);
        return new ResultVO<>(flag);
    }
}
